package resources;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.plaf.ListUI;

import parser.ParseException;
import parser.Parser;
import parser.ParserConstants;
import parser.ParserTokenManager;
import parser.SimpleCharStream;
import recovery.ParseEOFException;
import resources.exception.ProgramaException;
import semantic.Semantic;
import src.Main;

public class Compilador {

	public Parser parser;
	ParserTokenManager manager;

	public Compilador(InputStream stream) {
		parser = new Parser(stream);
		manager = new ParserTokenManager(new SimpleCharStream(stream));
	}

	public void compilar() {
			boolean debug_as = true;
			parser.debug_recovery = true;

			if(!debug_as) parser.disable_tracing();
		    try {
		      parser.programa();
		    }
		    catch (ParseEOFException e)
		    {
		      System.err.println(e.getMessage());
		    } catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();			
			}finally{
				  if (parser.token_source.foundLexError() != 0) {
					  Main.window.getTxtConsole().append("### ERROS LEXICOS ###\n\n");
		                for (int i = 0; i <parser.token_source.lexError.size(); i++) {
		                    Main.window.getTxtConsole().append(parser.token_source.lexError.get(i));
		                }
		            }
				  
				  if (parser.contParseError != 0) {
					  Main.window.getTxtConsole().append("### ERROS SINTATICOS ###\n\n");
		                for (int i = 0; i <parser.contParseError; i++) {
		                    Main.window.getTxtConsole().append(parser.synError.get(i));
		                }
		            }
				Main.window.getTxtConsole().append(parser.token_source.foundLexError() + " erro(s) lexicos encontrado(s)\n");
				Main.window.getTxtConsole().append(parser.contParseError + " erro(s) sintaticos encontrado(s)");
			}
	}
	
	public Semantic getSemantic(){
		return parser.semantic;
	}
}